package com.turing.post.organization.impl;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.SysBranch;
import com.turing.post.organization.util.SysBranchDao;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * @author zxp
 * @Dec 18, 2012
 */
@Stateless(mappedName = "organization.impl.SysBranchDaoImpl")
@Remote
@Local
public class SysBranchDaoImpl implements SysBranchDao {
	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public SysBranch getSysBranchByBranchNo(String branchNo) {
		return _em.find(SysBranch.class, branchNo);
	}

	@Override
	public void insertSysBranch(SysBranch sysBranch)
			throws PCPSApplicationException {
		// 新增时顺序号默认为0
		sysBranch.setOrderSeqNo(0);
		GeneralDao.insert(_em, sysBranch);
	}

	@Override
	public void updateSysBranch(SysBranch sysBranch)
			throws PCPSApplicationException {
		GeneralDao.update(_em, sysBranch);
	}

	@Override
	public void deleteSysBranch(String branchNo)
			throws PCPSApplicationException {
		try {
			String sql = "delete from SysBranch where BranchNo='" + branchNo
					+ "'";
			Query query = _em.createNativeQuery(sql);
			query.executeUpdate();
		} catch (Exception e) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_DATABASE,
					"delete", "该机构已被引用，禁止删除!", false);
		}
	}

	@Override
	public Integer updateSysBranchOrderSeqNo(String saleBranchNo) {
		SysBranch sy = getSysBranchByBranchNo(saleBranchNo);
		Integer res = 0;
		res = sy.getOrderSeqNo() + 1;
		sy.setOrderSeqNo(res);
		try {
			updateSysBranch(sy);
		} catch (PCPSApplicationException e) {
			e.printStackTrace();
		}
		return getSysBranchByBranchNo(saleBranchNo).getOrderSeqNo();
	}

	/**
	 * 复位机构业务单顺序号
	 * 
	 * @param issueCompanyNo
	 * @throws PCPSApplicationException
	 */
	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void resetSysBranchSeqNoByIssueCompanyNo(String issueCompanyNo)
			throws PCPSApplicationException {
		String sql = "update SysBranch set OrderSeqNo = 0 where BranchNo like ?1";
		Query query = _em.createNativeQuery(sql);
		if (PCPSUtil.isEmpty(issueCompanyNo) || issueCompanyNo.length() != 6)
			throw new PCPSApplicationException("", "发卡商" + issueCompanyNo
					+ "为空或非法");
		query.setParameter(1, issueCompanyNo + "%");
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getSysBrchInfoBySubBranchNos(String subBranchNos) {
		String sql = "select t.brchno, t.brchname, t.brchtype from ("
				+ " select i.issuebranchno as brchno,i.issuebranchname as brchname,'11' as brchtype from ISSUEBRANCH i where i.issuebranchno in"
				+ " (select t.subbranchno from SYSBRANCHSUBORD t where t.branchno = ?1 and t.subbranchsuperno = ?1)"
				+ " union all"
				+ " select s.salebranchno as brchno,s.salebranchname as brchname,'12' as brchtype from SALEBRANCH s where s.salebranchno in"
				+ " (select t.subbranchno from SYSBRANCHSUBORD t where t.branchno = ?1 and t.subbranchsuperno = ?1)"
				+ " ) t order by t.brchno,t.brchtype desc";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, subBranchNos);
		List<Object[]> obj_list = query.getResultList();
		return obj_list;
	}
}
